package com.ruoyi.bysj.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.bysj.domain.SysGroupsUser;
import com.ruoyi.bysj.mapper.SysGroupsUserMapper;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bysj.mapper.SysGroupsMapper;
import com.ruoyi.bysj.domain.SysGroups;
import com.ruoyi.bysj.service.ISysGroupsService;

/**
 * 维修分组Service业务层处理
 * 
 * @author laow
 * @date 2021-10-17
 */
@Service
public class SysGroupsServiceImpl implements ISysGroupsService 
{
    @Autowired
    private SysGroupsMapper sysGroupsMapper;

    @Autowired
    private SysGroupsUserMapper sysGroupsUserMapper;

    /**
     * 查询维修分组
     * 
     * @param groupId 维修分组主键
     * @return 维修分组
     */
    @Override
    public SysGroups selectSysGroupsByGroupId(Long groupId)
    {
        return sysGroupsMapper.selectSysGroupsByGroupId(groupId);
    }

    /**
     * 查询维修分组列表
     * 
     * @param sysGroups 维修分组
     * @return 维修分组
     */
    @Override
    public List<SysGroups> selectSysGroupsList(SysGroups sysGroups)
    {
        return sysGroupsMapper.selectSysGroupsList(sysGroups);
    }

    /**
     * 新增维修分组
     * 
     * @param sysGroups 维修分组
     * @return 结果
     */
    @Override
    public int insertSysGroups(SysGroups sysGroups)
    {
        int rows = sysGroupsMapper.insertSysGroups(sysGroups);
        insertGroupsUser(sysGroups);
        return rows;
    }

    /**
     * 修改维修分组
     * 
     * @param sysGroups 维修分组
     * @return 结果
     */
    @Override
    public int updateSysGroups(SysGroups sysGroups)
    {
        return sysGroupsMapper.updateSysGroups(sysGroups);
    }

    /**
     * 批量删除维修分组
     * 
     * @param groupIds 需要删除的维修分组主键
     * @return 结果
     */
    @Override
    public int deleteSysGroupsByGroupIds(Long[] groupIds)
    {
        return sysGroupsMapper.deleteSysGroupsByGroupIds(groupIds);
    }

    /**
     * 删除维修分组信息
     * 
     * @param groupId 维修分组主键
     * @return 结果
     */
    @Override
    public int deleteSysGroupsByGroupId(Long groupId)
    {
        return sysGroupsMapper.deleteSysGroupsByGroupId(groupId);
    }
    public void insertGroupsUser(SysGroups sysGroups)
    {
        List<String> userNames = sysGroups.getUserNames();
        if (StringUtils.isNotNull(userNames))
        {
            List<SysGroupsUser> list = new ArrayList<SysGroupsUser>();
            for (String userName : userNames) {
                SysGroupsUser gu = new SysGroupsUser();
                gu.setGroupId(sysGroups.getGroupId());
                gu.setUserName(userName);
                list.add(gu);
            }
            if (list.size() > 0)
            {
                sysGroupsUserMapper.batchGroupsUser(list);
            }
        }
    }
}
